package co.com.ias;

import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.view.MotionEvent;

public class ActivityIAS extends Activity {
	
	private GLSurfaceView glSurfaceView;
	private RendererIAS rendererIAS;
	private FisicaIAS fisicaIAS;
	
	private Timer timer;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        fisicaIAS = new FisicaIAS();
        
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        
        rendererIAS = new RendererIAS(this, fisicaIAS);
        
        glSurfaceView = new GLSurfaceView(this);
        glSurfaceView.setRenderer(rendererIAS);
        setContentView(glSurfaceView);
    }
    
    @Override
    public boolean onTouchEvent(MotionEvent e) {
    	if (e.getActionMasked() == MotionEvent.ACTION_DOWN) {
        	if (rendererIAS != null && fisicaIAS != null) {
        		fisicaIAS.empujado = true;
        	}
        	return true;
    	} else {
        	return false;
    	}
    }
    
    @Override
    public void onPause() {
    	glSurfaceView.onPause();
    	super.onPause();
    	detenerFisica();
    }
    
    @Override
    public void onResume() {
    	glSurfaceView.onResume();
    	super.onResume();
    	iniciarFisica();
    }
    
    @Override
    public void onStart() {
    	super.onStart();
    }
    
    @Override
    public void onStop() {
    	super.onStop();
    }
    
    private void iniciarFisica() {
    	
		timer = new Timer();
    	
		fisicaIAS.deltaTiempo = 0.015f;
		fisicaIAS.detenerFisica = false;
		
    	timer.schedule(new TimerTask() {
			
			@Override
			public void run() {
				fisicaIAS.actualizarValores();
			}
		}, 0, 15);
    }
    
    private void detenerFisica() {
    	timer.cancel();
    	timer.purge();
    	
    	fisicaIAS.detenerFisica = true;
    }
}